# coding:utf-8
# @Time    : 2021/11/29 17:38
# @Author  : Martin
from page_object.base.page_items import PageItems
from page_object.base.base_page import BasePage
from util.read_file import get_approve_data


class CombinePackageJudge(BasePage):
    _base_url = "/nspas-web-in/orderPlummetCreate.htm"

    def select_company(self, txt):
        """
        :param txt: 公司类型
        :return:
        """
        self.select('id', 'activityChannel', '2')
        self.select('id', 'comType', txt)
        self.click('link text', '确定')
        return CombinePackagePage(self.driver, self.logger, txt)


class CombinePackagePage(PageItems):
    def __new__(cls, *args, **kwargs):

        instance = super().__new__(cls)
        instance._base_url = "/nspas-web-in/activityCreateFilter.do?activityType" \
                             f"=COMCOMBINEACTIVITY&promotionActType=null&activityChannel={args[2]}&comType={args[3]}"
        return instance

    def __init__(self, driver, logger, ch_type, com_type):

        super().__init__(driver, logger)

    def input_act_scope(self, params):
        """
        输入固定搭配
        :param params:
        :return:
        """
        # 点击固定搭配
        self.click('id', 'fixedCombineRadio')

        for idx, param in enumerate(params):
            self.select('id', 'combine-bonustype', param['bonus_type'])
            # 点击增加
            self.click('xpath', '//input[@onclick="tableAdd()"]')
            # index = params.index(param) + 1
            # 输入固定搭配编码
            self.input('css selector',
                       f'#comBonusCmmdTableContent>div:nth-child({idx + 1}) input[name="packageCmmdCode"]', param['code'])
            self.click('css selector',
                       f'#comBonusCmmdTableContent>div:nth-child({idx + 1}) input[name="packageCmmdName"]')
            sub_table = self.locate('css selector', f'#comBonusCmmdTableContent>div:nth-child({idx + 1})>div>table>tbody')

            for sub_idx, sub in enumerate(param['subs']):
                # 集团限价
                limit_col = sub_table.find_elements('tag name', 'tr')[sub_idx].find_elements('tag name', 'td')[3].find_element('tag name', 'input')
                self.input_ele(limit_col, sub['limit_price'])
                # 活动价
                act_col = sub_table.find_elements('tag name', 'tr')[sub_idx].find_elements('tag name', 'td')[6].find_element('tag name', 'input')
                self.input_ele(act_col, sub['act_price'])

    def go_to_res_manage(self):
        """
        进入资源管理页面
        :return:
        """
        self.click('id', 'submitBtn')

    def input_last_message(self):
        """
        重写父类方法，固定搭配审批结束不需要输入文本
        :return:
        """
        pass

    # def click_approve_confirm(self):
    #     self.switch_frame('layui-layer-iframe1')
    #     self.click('name','Submit32')
    #     self.switch_default()


    @staticmethod
    def get_approve_area_create():
        """
        获取总部新建审批流账号数据
        :return:
        """
        return get_approve_data('online_combine_package', 'head', 'create')

    def go_to_approve(self):
        self.click('xpath', '//input[@onclick="resourceConfirmOk();"]')
        self.wait(20)
        self.click('id', 'comfireBtn')


if __name__ == '__main__':
    pass
